package controller.backOffice;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.CellEditEvent;

import ejb.category.CategoryService;
import entities.Category;

@Named
@RequestScoped
public class CategoryController implements Serializable{

	private static final long serialVersionUID = 1L;

	@Inject
	private Category currentCategory;


	@EJB
	private CategoryService categoryService;


	private List<Category> categories;
	
	private Category selectedCategory;

	private String text = "PrimeFaces";  

	public String getText() {  
		return text;  
	}  

	public void setText(String text) {  
		this.text = text;  
	}

	public String add() {
		categoryService.create(currentCategory);
		return "listeCategoriesBO";
	}

	public String addNewCategory(){
		return "addNewCategory";
	}
	
	public String gestionCategory(){
		return "listeCategoriesBO";
	}

	public String selectCategory (Long id){
		System.out.println("lol");
		System.out.println(id);
		selectedCategory=categoryService.find(id);
		System.out.println(selectedCategory.getTitle());
		return "categorieProduit";
	}
	
	public String editer(){
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String categoryId1 = (String)requestMap.get("categoryId");  
		Long categoryId = Long.parseLong(categoryId1);		
		currentCategory=categoryService.find(categoryId);
		return "editCategory";
	}

	public String edit1(){
		Category cat = new Category();
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String categoryId1 = (String)requestMap.get("categoryId");  
		Long categoryId = Long.parseLong(categoryId1);		
		cat=categoryService.find(categoryId);
		cat.setTitle(currentCategory.getTitle());
		categoryService.update(cat);
		return "listeCategoriesBO";
	}



	public String delete() { 
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String categoryId1 = (String)requestMap.get("categoryId");  
		Long categoryId = Long.parseLong(categoryId1);
		if (categoryId == null ){
			return "welcome";	
		}
		else if (categoryId != null){
			categoryService.delete(categoryId);	
		}
		return "back/categorie/index.xhtml";
	}


	public List<Category> getCategories() {
		return categoryService.findAll();
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public Category getCurrentCategory() {
		return currentCategory;
	}

	public void setCurrentCategory(Category currentCategory) {
		this.currentCategory = currentCategory;
	}

	public Category getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(Category selectedCategory) {
		this.selectedCategory = selectedCategory;
	}
	
	public String selectionCategory() {
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String categoryId1 = (String)requestMap.get("categoryId");  
		Long categoryId = Long.parseLong(categoryId1);
		selectedCategory = categoryService.find(categoryId);
		return "produitCategorie";
	}

}
